package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
import models.Course;
import models.User;
import tools.Checker;
import tools.Encryptor;

public class NewCourseController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/newCourse.jsp");
        dispatcher.forward(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        String courseName = "", courseLanguage = "", courseDescription = "", address = "";
        boolean finaltest = false, isEmpty = false;
        double priceD = 0.0;
        String baseDir = "C:/PDL/";
        HttpSession sessie = request.getSession();
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");

        if (!request.getParameter("courseName").isEmpty()) {
            courseName = request.getParameter("courseName");
            request.setAttribute("courseName", courseName);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError1());
        }
        if (!request.getParameter("courseLanguage").isEmpty()) {
            courseLanguage = request.getParameter("courseLanguage");
            request.setAttribute("courseLanguage", courseLanguage);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError3());
        }
        if (!request.getParameter("courseDescription").isEmpty()) {
            courseDescription = request.getParameter("courseDescription");
            request.setAttribute("courseDescription", courseDescription);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError2());
        }
        if (request.getParameter("finalTest") != null) {
            finaltest = true;
        }
        if (!request.getParameter("price").isEmpty()) {
            Checker checker = new Checker();
            String priceS = request.getParameter("price"), priceS2 = "";
            for (int i = 0; i < priceS.length(); i++) {
                char temp;
                temp = priceS.charAt(i);
                if(temp == ',') {
                    temp = '.';
                }
                priceS2= priceS2 + temp;
            }
            if (checker.isDigit(priceS2)) {
                priceD = Double.parseDouble(priceS2);
            } else {
                request.setAttribute("foutMelding", inhoud.getError5());
                isEmpty = true;
            }
            request.setAttribute("coursePrice", priceS2);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError4());
        }


        if (!isEmpty) {
            Course course = new Course(courseName, courseLanguage, courseDescription, finaltest, priceD);
            int lastID = queryManager.addCourse(course);
            if(lastID>0){
                File file = new File(baseDir + lastID);
                if(!file.exists()){
                    file.mkdirs();
                }
            }      
            address = "/";
        } else {
            address = "/newCourse.jsp";
        }
        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);

        dispatcher.forward(request, response);
    }
}
